package com.app.shop.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.app.shop.config.security.DgbSecurityUserHelper;
import com.app.shop.controller.system.BaseController;
import com.app.shop.entity.SysAuth;
import com.app.shop.entity.SysReAuthUser;
import com.app.shop.entity.SysUser;
import com.app.shop.entity.query.SysUserQuery;
import com.app.shop.entity.show.BaseShow;
import com.app.shop.entity.show.SysUserShow;
import com.app.shop.service.system.SysAuthService;
import com.app.shop.service.system.SysReAuthUserService;
import com.app.shop.service.system.SysUserService;
import com.app.shop.util.*;
import com.github.pagehelper.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 首页数据展示处理Controller模块
 *
 * @author chenliwei
 * @version v1.0 2017-11-5
 */
@Controller
@RequestMapping(value = "/api/user")
public class WxUserController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(WxUserController.class);

    /**
     * 微信登录（登陆后即可访问订单，以及个人信息）
     * @param jsonObject
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/getOpenId")
    public ApiRest getDictList(@RequestBody JSONObject jsonObject)
            throws IOException {

        log.debug(jsonObject.toString());
        // "jsCode":"061aXsiY1jIdx01xBHhY19sCiY1aXsiC",
        // "nickName":"往惜",
        // "sign":"31ce0f53d315d1ee0c36dfc5825225f4",
        // "time":"20190404161047"
        // "avatarUrl":"xxxxxx"
        JSONObject res= WxUtil.getWxUserOpenid(jsonObject.getString("code"));
        if(res!=null){
            //将用户存储到数据库，如存在就不存储
            //res.getString("openid")   openid
            //jsonObject.getString("avatarUrl") 用户头像
            //jsonObject.getString("nickName")  用户名称
            //生成令牌
            //res.put("openid",res.getString("openid"));
            //将用户id记住插入表中，生产token有效8小时
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE,res);
        }else{
            return new ApiRest(Constant.REST_ERROR_CODE, Constant.ERROR_MESSAGE,null);
        }

    }

    /**
     * 预览H5
     * @return
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping(value = "/getH5")
    public ApiRest getH5()
            throws IOException {

            Map map=new HashMap();
            map.put("openid","test");
            map.put("nickName","预览账号");
            map.put("avatarUrl","https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTJ6NApuHciciaGASkteklbGuoyrxIaJd1BNXsGS8XMMzB179qMQGf7aI3dj81SHficXuQTjNSItibUwhw/132");
            map.put("address","中关村大街甲10号银海大厦北区B1");
            //将用户id记住插入表中，生产token有效8小时
            return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE,map);

    }

    /**
     * 所有操作以openid为主Id
     * @param jsCode
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getUserInfo")
    public ApiRest getDictList(String jsCode) {

        return new ApiRest(Constant.REST_SUCCESS_CODE, Constant.SUCCESS_MESSAGE, "调用成功");
    }

}